package com.gyj.zookeeper.Watcher;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Gao on 2018/10/22.
 */
public class WatcherExample implements Watcher {

    private static CountDownLatch latch = new CountDownLatch(1);
    private String zkpath = "localhost:2181";

    @Override
    public void process(WatchedEvent event) {
        System.out.println("watcher=" + this.getClass().getName());
        System.out.println("path=" + event.getPath());
        System.out.println("eventType=" + event.getType().name());

//        try {
//            latch.await();
//            System.out.println("复制数据完毕");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        latch.countDown();
        System.out.println("继续执行。。。");
    }

    public String getZkpath() {
        return zkpath;
    }

    public void setZkpath(String zkpath) {
        this.zkpath = zkpath;
    }

    public static void main(String[] args) {
        WatcherExample test = new WatcherExample();
        try {
            ZooKeeper zk = new ZooKeeper(test.getZkpath(), 10000, test);
            List<String> children = zk.getChildren("/node2", true);//监听节点变化，子节点信息变化的时候才会监听
            System.out.println(children);
            //Thread.sleep(1000 * 60 * 60);
            //latch.countDown();
        } catch (IOException | KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
